package aor2.cpu;

import aor2.components.Clock;
import aor2.components.Restorable;

public class ClockCPU extends Clock implements Cloneable, Restorable {

	private static ClockCPU singleton = null;

	private ClockCPU() {
		super(0, 0, 2);
	}

	public static ClockCPU getInstance() {
		if (singleton == null)
			singleton = new ClockCPU();
		return singleton;
	}

	@Override
	public ClockCPU clone() {
		ClockCPU clk = (ClockCPU) super.clone();
		return clk;
	}

	@Override
	public String toString() {
		return "ClockCPU[ T:" + super.getT() + " ,P:" + super.getPhase() + "]";
	}

	@Override
	public void restore(String restorePoint) {
		String trim = restorePoint.replace(beginTag(), "");
		trim = trim.replace(endTag(), "");
		trim = trim.trim();
		setCurrentClkPart(Integer.parseInt(trim));
	}

	@Override
	public String createRestorePoint() {
		return beginTag() + "\n\t" + getCurrentClkPart() + "\n" + endTag();
	}

	@Override
	public String beginTag() {
		return "<CPUclock>";
	}

	@Override
	public String endTag() {
		return "</CPUclock>";
	}

	public static void main(String[] args) {
		ClockCPU clock = ClockCPU.getInstance();

		String restorePoint = clock.createRestorePoint();

		System.out.println(restorePoint);

		clock.nextClkPart();
		clock.nextClkPart();
		clock.nextClkPart();
		clock.nextClkPart();
		clock.nextClkPart();

		clock.restore(restorePoint);

		restorePoint = clock.createRestorePoint();

		System.out.println(restorePoint);

	}
}
